/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogSettings.java
 *
 * Created on 02.06.2011, 18:06:32
 */
package ru.icq.gui.dialogs;

import java.awt.CardLayout;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.apache.log4j.Logger;
import ru.icq.gui.dialogs.Renderer.IconListRenderer;
import ru.icq.gui.frame.FrameICQ;
import ru.icq.io.loadTheme;
import ru.icq.utils.TreeObject;

/**
 * Окно настроек приложения.
 * @author Носов А.В.
 */
public class DialogSettings extends javax.swing.JDialog {

    // Variables declaration
    private static final Logger log = Logger.getLogger(DialogSettings.class);
    
    private DefaultListModel modelList;
    private DefaultMutableTreeNode modelNade;
    
    private FrameICQ parent;
    private loadTheme theme = new loadTheme("Default", "Russian");
    // End of variables declaration

    /** Creates new form DialogSettings */
    public DialogSettings(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (FrameICQ) parent;
        
        initComponents();
        
        setTheme(theme);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelTree = new javax.swing.JPanel();
        scrollPaneTreeSettings = new javax.swing.JScrollPane();
        treeSettings = new javax.swing.JTree();
        panelCard = new javax.swing.JPanel();
        panelLoginAccounts = new javax.swing.JPanel();
        scrollPaneAccounts = new javax.swing.JScrollPane();
        modelList = new DefaultListModel();
        listAccounts = new javax.swing.JList();
        panelAccounts = new javax.swing.JPanel();
        panelButtons = new javax.swing.JPanel();
        buttonAddAccounts = new javax.swing.JButton();
        buttonModifyAccount = new javax.swing.JButton();
        buttonDeleteAccount = new javax.swing.JButton();
        panelMain = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        panelSettingsButtons = new javax.swing.JPanel();
        panelEmptySettings = new javax.swing.JPanel();
        buttonSettingsOk = new javax.swing.JButton();
        buttonSettingsApply = new javax.swing.JButton();
        buttonSettingsCansel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelTree.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelTree.setLayout(new java.awt.BorderLayout());

        treeSettings.setModel(new DefaultTreeModel(settingsTree()));
        treeSettings.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        treeSettings.setRootVisible(false);
        int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
        treeSettings.getSelectionModel().setSelectionMode(mode);
        treeSettings.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeSettingsValueChanged(evt);
            }
        });
        scrollPaneTreeSettings.setViewportView(treeSettings);
        //panelTree.add(panelLoginAccounts, java.awt.BorderLayout.CENTER);
        //panelTree.add(panelMain, java.awt.BorderLayout.CENTER);

        panelTree.add(scrollPaneTreeSettings, java.awt.BorderLayout.CENTER);

        getContentPane().add(panelTree, java.awt.BorderLayout.LINE_START);

        panelCard.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelCard.setLayout(new java.awt.CardLayout());

        panelLoginAccounts.setLayout(new java.awt.BorderLayout());

        listAccounts.setModel(modelList);
        listAccounts.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listAccounts.setCellRenderer(new IconListRenderer(getIcon()));
        scrollPaneAccounts.setViewportView(listAccounts);

        panelLoginAccounts.add(scrollPaneAccounts, java.awt.BorderLayout.CENTER);

        panelAccounts.setLayout(new java.awt.BorderLayout());

        panelButtons.setLayout(new java.awt.GridBagLayout());

        buttonAddAccounts.setText("Add");
        buttonAddAccounts.setName("settings.addAccount"); // NOI18N
        buttonAddAccounts.setPreferredSize(new java.awt.Dimension(130, 29));
        buttonAddAccounts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddAccountsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 5, 3);
        panelButtons.add(buttonAddAccounts, gridBagConstraints);

        buttonModifyAccount.setText("Modify");
        buttonModifyAccount.setEnabled(false);
        buttonModifyAccount.setName("settings.modifyAccount"); // NOI18N
        buttonModifyAccount.setPreferredSize(new java.awt.Dimension(130, 29));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 5, 3);
        panelButtons.add(buttonModifyAccount, gridBagConstraints);

        buttonDeleteAccount.setText("Delete");
        buttonDeleteAccount.setEnabled(false);
        buttonDeleteAccount.setName("settings.deleteAccount"); // NOI18N
        buttonDeleteAccount.setPreferredSize(new java.awt.Dimension(130, 29));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 5, 3);
        panelButtons.add(buttonDeleteAccount, gridBagConstraints);

        panelAccounts.add(panelButtons, java.awt.BorderLayout.PAGE_START);

        panelLoginAccounts.add(panelAccounts, java.awt.BorderLayout.LINE_END);

        panelCard.add(panelLoginAccounts, "loginAccounts");

        jButton1.setText("jButton1");
        panelMain.add(jButton1);

        panelCard.add(panelMain, "settingsMain");

        getContentPane().add(panelCard, java.awt.BorderLayout.CENTER);

        panelSettingsButtons.setLayout(new java.awt.BorderLayout());

        panelEmptySettings.setLayout(new java.awt.GridBagLayout());

        buttonSettingsOk.setText("Ok");
        buttonSettingsOk.setName("settings.ok"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 5);
        panelEmptySettings.add(buttonSettingsOk, gridBagConstraints);

        buttonSettingsApply.setText("Apply");
        buttonSettingsApply.setName("settings.apply"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 5);
        panelEmptySettings.add(buttonSettingsApply, gridBagConstraints);

        buttonSettingsCansel.setText("Cansel");
        buttonSettingsCansel.setName("settings.cansel"); // NOI18N
        buttonSettingsCansel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSettingsCanselActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 5);
        panelEmptySettings.add(buttonSettingsCansel, gridBagConstraints);

        panelSettingsButtons.add(panelEmptySettings, java.awt.BorderLayout.LINE_END);

        getContentPane().add(panelSettingsButtons, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void treeSettingsValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeSettingsValueChanged
        TreePath path = treeSettings.getSelectionPath();
        if (path == null) return;
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
        TreeObject to = (TreeObject) selectedNode.getUserObject();
        CardLayout cl = (CardLayout)panelCard.getLayout();
        switch (to.getId()) {
            case 1: 
                cl.show(panelCard, "loginAccounts");
                break;
            case 2:
                cl.show(panelCard, "settingsMain");
                break;
            default:
                break;
        }
    }//GEN-LAST:event_treeSettingsValueChanged

    private void buttonSettingsCanselActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSettingsCanselActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_buttonSettingsCanselActionPerformed

    private void buttonAddAccountsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddAccountsActionPerformed
        DialogSelectAccount sa = new DialogSelectAccount(parent, true);
        sa.setTheme(theme);
        sa.setVisible(true);
    }//GEN-LAST:event_buttonAddAccountsActionPerformed

    /**
     * Создание модели дерева настроек.
     * @return модель дерева настроек
     */
    private TreeNode settingsTree() {
        // TODO Незнаю как прально сделать.
        
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("settings");
        
        TreeObject la = new TreeObject(1, "Login accounts");
        DefaultMutableTreeNode loginAccounts = new DefaultMutableTreeNode(la);
        if (theme.getLanguage("settings.loginAccounts.text") != null)
            la.setName(theme.getLanguage("settings.loginAccounts.text"));
        root.add(loginAccounts);
        
        TreeObject m = new TreeObject(2, "Main");
        DefaultMutableTreeNode main = new DefaultMutableTreeNode(m);
        if (theme.getLanguage("settings.main.text") != null)
            m.setName(theme.getLanguage("settings.main.text"));
        root.add(main);
        
        return root;
    }

    private Map<Object, Icon> getIcon() {
        Map<Object, Icon> icons = new HashMap<Object, Icon>();
        icons.put("icq", theme.getIcon("account.icq"));
        icons.put("jabber", theme.getIcon("account.jabber"));
        icons.put("vkontakte", theme.getIcon("account.vkontakte"));
//        icons.put("mrim", null);
//        icons.put("irc", null);
        return icons;
    }

    /**
     * Устанвливает тему.
     * @param theme класс темы
     */
    public void setTheme(loadTheme theme) {
        if (theme != null) this.theme = theme;
        this.theme.setTheme( (Component)this );
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DialogSettings dialog = new DialogSettings(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAddAccounts;
    private javax.swing.JButton buttonDeleteAccount;
    private javax.swing.JButton buttonModifyAccount;
    private javax.swing.JButton buttonSettingsApply;
    private javax.swing.JButton buttonSettingsCansel;
    private javax.swing.JButton buttonSettingsOk;
    private javax.swing.JButton jButton1;
    private javax.swing.JList listAccounts;
    private javax.swing.JPanel panelAccounts;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelCard;
    private javax.swing.JPanel panelEmptySettings;
    private javax.swing.JPanel panelLoginAccounts;
    private javax.swing.JPanel panelMain;
    private javax.swing.JPanel panelSettingsButtons;
    private javax.swing.JPanel panelTree;
    private javax.swing.JScrollPane scrollPaneAccounts;
    private javax.swing.JScrollPane scrollPaneTreeSettings;
    private javax.swing.JTree treeSettings;
    // End of variables declaration//GEN-END:variables
}
